package com.chinasoft.mall.activity.subActivity.goods;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.BaseActivity;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.adapter.GallaryImageAdapter;
import com.chinasoft.mall.activity.adapter.GallaryImageAdapter.ViewInfo;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.EmployeeGoodsByClass;
import com.chinasoft.mall.model.response.EpgBean;
import com.chinasoft.mall.model.response.GetGoodGrpImgByIdBean;
import com.chinasoft.mall.model.response.GoodsByClassBean;
import com.chinasoft.mall.model.response.GoodsGrpBean;
import com.chinasoft.mall.model.response.GoodsImgBean;
import com.chinasoft.mall.model.response.RechnBean;
import com.chinasoft.mall.model.response.SearchGoodsBean;
import com.chinasoft.mall.util.LocalDataCommend;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NewsScrollView;

/**
 * 商品详情
 * 
 * @author Administrator
 */
public class GoodsDetailActivity extends BaseActivity implements OnClickListener {
	// 将所属分类的商品全部显示出来
	private BaseBean goodsimg_bb;
	private BaseBean emplyeegoodsimg_bb;
	private BaseBean getgrp_bb;
	// 根据商品ID获取商品详细信息
	private BaseBean goodsbyid_bb;
	private Button phonebuy;
	private Button collect;
	private LinearLayout _ll;
	private List<ViewInfo> listViews;

	private GallaryImageAdapter giAdapter;
	private ViewPager gallery;

	private GoodsByClassBean gbcb;
	private EmployeeGoodsByClass egbc;
	private WebView webView;
	private LayoutInflater mInflater;
	private NewsScrollView scrollid;
	private View v ;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			setContentView(v);
			ControllerCenter.LoadOverDialog();
		}
	};

	@Override	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.sp_detail);
		setContentView(R.layout.blank);
		v =initView();
		ControllerCenter.LoadingDialog();
		initRequest();
		
	}
		

	private void initRequest() {
		/*
		 * goodsimg_bb = ControllerCenter.responsdata.get(Config.GET_GOODSIMG);
		 * if (goodsimg_bb != null) { success(goodsimg_bb); } else
		 */{
			goodsimg_bb = new BaseBean(Config.GET_GOODSIMG);
			emplyeegoodsimg_bb = new BaseBean(Config.GET_GOODSIMG);
			if (sz != null) {
				gbcb = new GoodsByClassBean();
				if (sz instanceof RechnBean) {
					RechnBean rb = (RechnBean) sz;
					gbcb.setGOODS_ID(rb.getID());
					gbcb.setPUB_DATE(rb.getPUB_DATE());
					gbcb.setGOODS_NAME(rb.getTITLE());
					goodsimg_bb.reqData.put("goods_id", rb.getID());
					if (rb.getTYPE() != null && rb.getTYPE().equals("3")) {
						getgrp_bb = new BaseBean(Config.GET_GOODSGRPIMGBYID);
						getgrp_bb.reqData.put("id", rb.getID());
						control.postMessage(getgrp_bb);
					} else {
						control.postMessage(goodsimg_bb);
					}
				} else if (sz instanceof EpgBean) {
					EpgBean rb = (EpgBean) sz;
					gbcb.setGOODS_ID(rb.getGOODS_ID());
					gbcb.setGOODS_NAME(rb.getGOODS_NAME());
					gbcb.setMPRC(rb.getMPRC());
					gbcb.setPRC(rb.getPRC());
					goodsimg_bb.reqData.put("goods_id", rb.getGOODS_ID());
					control.postMessage(goodsimg_bb);
				} else if (sz instanceof SearchGoodsBean) {
					SearchGoodsBean rb = (SearchGoodsBean) sz;
					gbcb.setGOODS_ID(rb.getGOODS_ID());
					gbcb.setGOODS_NAME(rb.getGOODS_NAME());
					gbcb.setMPRC(rb.getMPRC());
					gbcb.setPRC(rb.getPRC());
					gbcb.setTHUM(rb.getTHUM());
					gbcb.setCLASS_ID(rb.getCLASS_ID());
					gbcb.setValid(rb.getValid());
					goodsimg_bb.reqData.put("goods_id", rb.getGOODS_ID());
					control.postMessage(goodsimg_bb);
				} else if (sz instanceof GoodsByClassBean) {
					GoodsByClassBean rb = (GoodsByClassBean) sz;
					gbcb = rb;
					goodsimg_bb.reqData.put("goods_id", rb.getGOODS_ID());
					control.postMessage(goodsimg_bb);
				} else if (sz instanceof GoodsGrpBean) {
					GoodsGrpBean rb = (GoodsGrpBean) sz;
					gbcb.setGOODS_ID(rb.getGOODS_GRP_ID());
					gbcb.setGOODS_NAME(rb.getGOODS_GRP_NAME());
					gbcb.setMPRC(rb.getMPRC());
					gbcb.setPRC(rb.getPRC());
					gbcb.setTHUM(rb.getTHUM());
					goodsimg_bb.reqData.put("goods_id", rb.getGOODS_GRP_ID());
					control.postMessage(goodsimg_bb);
				} else if (sz instanceof EmployeeGoodsByClass) {

					EmployeeGoodsByClass rb = (EmployeeGoodsByClass) sz;
					gbcb.setGOODS_ID(rb.getE_GOODS_ID());
					gbcb.setGOODS_NAME(rb.getE_GOODS_NAME());
					gbcb.setMPRC(rb.getMARKET_PRICE());
					gbcb.setPRC(rb.getDISCOUNT_PRICE());
					gbcb.setTHUM(rb.getTHUM());
					emplyeegoodsimg_bb.reqData.put("goods_id", rb.getE_GOODS_ID());
					control.postMessage(emplyeegoodsimg_bb);
				}

				goodsbyid_bb = new BaseBean(Config.GET_GOODSBYID);
				goodsbyid_bb.reqData.put("goods_id", gbcb.getGOODS_ID());
				control.postMessage(goodsbyid_bb);
			} else {
				Toast.makeText(this, "数据获取失败", Toast.LENGTH_LONG).show();
				((MainActivity) this.getParent()).backCurrentActivity(false);
			}
		}
	}

	private View initView() {
		View view = LayoutInflater.from(this).inflate(R.layout.sp_detail, null);
		scrollid = (NewsScrollView) view.findViewById(R.id.scrollid);
		scrollid.scrollTo(0, 0);
		webView = (WebView) view.findViewById(R.id.spdetail_wv);
		_ll = (LinearLayout) view.findViewById(R.id._ll);
		phonebuy = (Button) view.findViewById(R.id.phonebuy);
		collect = (Button) view.findViewById(R.id.collect);
		gallery = (ViewPager) view.findViewById(R.id.gallery);
		listViews = new ArrayList<ViewInfo>();
		giAdapter = new GallaryImageAdapter(this, listViews);
		gallery.setAdapter(giAdapter);
		phonebuy.setOnClickListener(this);
		collect.setOnClickListener(this);
		mInflater = getLayoutInflater();
		gallery.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				for (int i = 0; i < _ll.getChildCount(); i++) {
					if (i == arg0) {
						((ImageView) _ll.getChildAt(i))
								.setImageResource(R.drawable.blackpoint);
					} else {
						((ImageView) _ll.getChildAt(i))
								.setImageResource(R.drawable.graypoint);
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		// gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
		//
		// @Override
		// public void onItemSelected(AdapterView<?> arg0, View arg1,
		// int arg2, long arg3) {
		// // imageViewList.get(arg2).setBackgroundResource(
		// // R.drawable.news_gallery_circle_select);
		//
		// for (int i = 0; i < _ll.getChildCount(); i++) {
		// if (i == arg2) {
		// ((ImageView) _ll.getChildAt(i))
		// .setImageResource(R.drawable.blackpoint);
		// } else {
		// ((ImageView) _ll.getChildAt(i))
		// .setImageResource(R.drawable.graypoint);
		// }
		// }
		// }
		//
		// @Override
		// public void onNothingSelected(AdapterView<?> arg0) {
		// }
		// });
		return view;
	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(goodsimg_bb)) {
			List<GoodsImgBean> _ebs = (List<GoodsImgBean>) obj.getResData()
					.getData();
			if (_ebs != null) {
				for (GoodsImgBean eb : _ebs) {
					ViewInfo vi = this.giAdapter.new ViewInfo();
					vi.view = mInflater.inflate(R.layout.spdetailgv, null);
					vi.eb = eb;
					listViews.add(vi);
				}
				giAdapter.setmListViews(listViews);
				for (int i = 0; i < giAdapter.getCount(); i++) {
					ImageView point = (ImageView) LayoutInflater.from(this)
							.inflate(R.layout.point, null);
					_ll.addView(point, i);
				}
			}
		} else if (obj.equals(getgrp_bb)) {
			List<GetGoodGrpImgByIdBean> _ebs = (List<GetGoodGrpImgByIdBean>) obj.getResData()
					.getData();
			if (_ebs != null) {
				for (GetGoodGrpImgByIdBean eb : _ebs) {
					ViewInfo vi = this.giAdapter.new ViewInfo();
					vi.view = mInflater.inflate(R.layout.spdetailgv, null);
					vi.egb = eb;
					listViews.add(vi);
				}
				giAdapter.setmListViews(listViews);
				for (int i = 0; i < giAdapter.getCount(); i++) {
					ImageView point = (ImageView) LayoutInflater.from(this)
							.inflate(R.layout.point, null);
					_ll.addView(point, i);
				}
				
			}
		} else if (goodsbyid_bb != null && obj.equals(goodsbyid_bb)) {
			List<GoodsByClassBean> gbcs = (List<GoodsByClassBean>) goodsbyid_bb
					.getResData().getData();
			if (gbcs != null && gbcs.size() > 0) {
				gbcb = gbcs.get(0);
				sz = gbcb;
				initWebView(StringUtil.grpDetailUrl(gbcb.getGOODS_ID(),
						gbcb.getCONTENT()));
				// this.onClick(collect);
			}
			handler.sendEmptyMessage(0);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.phonebuy:
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ "4006806278"));
			startActivity(intent);
			break;
		case R.id.collect:
			/*
			 * if (sz instanceof RechnBean) { goodsbyid_bb=new
			 * BaseBean(Config.GET_GOODSBYID);
			 * goodsbyid_bb.reqData.put("goods_id",((RechnBean)sz).getID());
			 * control.sendMessage(goodsbyid_bb); } else
			 */
		{
			if (LocalDataCommend.addGoodsBean(this, gbcb)) {
				Toast.makeText(this, "收藏成功", Toast.LENGTH_LONG).show();
				((MainActivity) this.getParent()).refreshLike();
			} else {
				Toast.makeText(this, "商品收藏失败", Toast.LENGTH_LONG).show();
			}
		}
			break;
		default:
			break;
		}
	}

	/**
	 * webview展示
	 * 
	 * @param url
	 *            访问地址
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void initWebView(final String url) {
//		ControllerCenter.LoadingDialog();
		webView.getSettings().setJavaScriptEnabled(true);
		webView.requestFocus();
		webView.setScrollBarStyle(0);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
		webView.loadUrl(url);
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
//				view.loadUrl(url);
				return true;
			}
		});
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {// 载入进度改变而触发
				super.onProgressChanged(view, progress);
				if (progress == 100) {
//					ControllerCenter.LoadOverDialog();
					scrollid.scrollTo(0, 0);
				}
			}
		});
	}
}
